package com.tl.commonlibrary.picture;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.util.DisplayMetrics;

import com.squareup.picasso.Picasso;
import com.tl.commonlibrary.network.NetConfig;
import com.tl.commonlibrary.tool.DeviceUtil;

import java.io.IOException;
import java.lang.ref.WeakReference;
import java.net.MalformedURLException;

/**
 * @author JongSung
 * @Description
 * @date 2017/11/24 0024
 */

public class ImageGetterThread extends Thread {
    private WeakReference<Activity> activityWR;
    private String html;
    private Handler handler;
    private int tag = -1;

    public ImageGetterThread(Activity activity) {
        activityWR = new WeakReference<Activity>(activity);
    }

    public String getHtml() {
        return html;
    }

    public ImageGetterThread setHtml(String html) {
        this.html = html;
        return this;
    }

    public Handler getHandler() {
        return handler;
    }

    public ImageGetterThread setHandler(Handler handler, int tag) {
        this.handler = handler;
        this.tag = tag;
        return this;
    }

    public void run() {
        if (activityWR != null) {
            final Activity activity = activityWR.get();
            if (activity != null) {

                final DisplayMetrics displayMetrics = DeviceUtil.getScreen(activity);
                // 这里不取屏幕的宽度，因为控件本身有padding，所以取控件实际的宽度，xml中设置的contentTView宽度边距为距左右各20dp，xml修改此处也应该修改
                final int contentTViewWidth = displayMetrics.widthPixels - DeviceUtil.dp_to_px(20, activity);
                final Html.ImageGetter imageGetter = new Html.ImageGetter() {

                    @Override
                    public Drawable getDrawable(String source) {
                        Drawable drawable = null;
                        try {
                            Bitmap bitmap = Picasso.with(activity).load(source).get();
                            drawable = new BitmapDrawable(bitmap);
                            if (drawable != null) {
                                int drawableWidth = drawable.getIntrinsicWidth();
                                int drawableHeight = drawable.getIntrinsicHeight();
                                //超出屏幕宽度
//                                if (drawableWidth > contentTViewWidth) {
                                    // 求出缩放比例，double类型缩放较int型精准
                                    double scale = contentTViewWidth / (1.0 * drawableWidth);
                                    int scaleWidth = (int) (drawableWidth * scale);
                                    int scaleHeight = (int) (drawableHeight * scale);
                                    int left = (contentTViewWidth - scaleWidth) / 2;
                                    // 通过left居中显示
                                    drawable.setBounds(left, 0, left + scaleWidth, scaleHeight);
//                                } else {
//                                    int left = (contentTViewWidth - drawableWidth) / 2;
//                                    // 通过left居中显示
//                                    drawable.setBounds(left, 0, left + drawableWidth, drawableHeight);
//
//                                }
                            }
                        } catch (MalformedURLException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                        return drawable;
                    }
                };
                CharSequence charSequence = Html.fromHtml(html, imageGetter, null);
                Message message = Message.obtain();
                message.obj = charSequence;
                message.arg1 = tag;
                handler.sendMessage(message);
            }
        }
        super.run();
    }
}
